package com.yoda.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.constant.MessageConstant;
import com.yoda.dto.AdvDto;
import com.yoda.dto.AdvPageQuery;
import com.yoda.entity.YddAdvpos;
import com.yoda.mapper.YddAdvposMapper;
import com.yoda.result.Result;
import com.yoda.service.IYddAdvposService;
import com.yoda.vo.AdvVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements IYddAdvposService {

    /**
     * 分页查询
     * @return
     */
    @Override
    public Result pageQuery(AdvPageQuery advPageQuery) {
        //判断传回来的是否为空
        if (advPageQuery.getPage() == null){
            advPageQuery.setPage(1+"");
        }
        if (advPageQuery.getLimit() == null){
            advPageQuery.setLimit(10+"");
        }
        //获取前端传回来的数据
        Integer p = Integer.valueOf(advPageQuery.getPage());
        Integer size = Integer.valueOf(advPageQuery.getLimit());
        //查询条件开始
        LambdaQueryWrapper<YddAdvpos> wrapper = Wrappers.lambdaQuery();
        wrapper.like(YddAdvpos::getAdvposname,advPageQuery.getAdvposname());
        //调用方法分页
        IPage<YddAdvpos> advposIPage = new Page<>(p,size);
        IPage<YddAdvpos> iPage = this.page(advposIPage,wrapper);
        //新建一个vo对象
        AdvVo vo = new AdvVo();
        vo.setCount((int) iPage.getPages());
        vo.setTotalPages((int) iPage.getTotal());
        vo.setPageSize(size);
        vo.setCurrentPage(p);
        vo.setData(iPage.getRecords());
        //返回数据
        return Result.success(vo);
    }

    /**
     * 广告位修改
     * @param dto
     * @return
     */
    @Override
    public Result updateAdv(AdvDto dto) {
        //判断传回来的id是否存在
        if (dto.getId() == null){
            return Result.error(MessageConstant.ACCOUNT_LOCKED);
        }
        //根据id查询到YddAdvpos的内容，复制之后再修改
        YddAdvpos advpos = this.getById(dto.getId());
        BeanUtils.copyProperties(dto,advpos);
        //开启条件判断，如果id不相等则不允许修改
        LambdaQueryWrapper<YddAdvpos> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(YddAdvpos::getId,dto.getId());
        //调用方法开始修改
        this.saveOrUpdate(advpos,wrapper);
        return Result.success();
    }

    /**
     * 广告位详情
     * @param id
     * @return
     */
    @Override
    public Result selectById(String id) {
        //强转类型，然后开始查询回显
        Integer advId = Integer.valueOf(id);
        YddAdvpos byId = this.getById(advId);
        return Result.success(byId);
    }

    /**
     * 广告位删除
     * @param advId
     * @return
     */
    @Override
    public Result deleteById(String advId) {
        if (advId == null){
            return Result.error(MessageConstant.DISH_ON_SALE);
        }
        //直接根据传回来的id进行删除
        Integer id = Integer.valueOf(advId);
        this.removeById(id);
        return Result.success();
    }


    /**
     * 广告位列表-全部
     * @return
     */
    @Override
    public Result selectAll() {
        //调用list方法直接查询全部
        List<YddAdvpos> list = this.list();
        return Result.success(list);
    }

    /**
     * 广告位添加
     * @param yddAdvpos
     * @return
     */
    @Override
    public Result addAdv(YddAdvpos yddAdvpos) {
        //直接调用方法直接插入
        this.save(yddAdvpos);
        return Result.success();
    }

}
